#include "CameraManager.h"
#include "ReportError.h"

CameraManager::CameraManager()
{
	m_ActiveCamera = 0;
	m_PreCamera = 0;
	//m_Frustum = 0;
}

void CameraManager::Update()
{
	m_ActiveCamera->Update();
}

void CameraManager::DrawPreCamPos()
{
	//m_ActiveCamera->Draw();
	if(m_PreCamera)
	m_PreCamera->Draw();
}

void CameraManager::SetCamera(const char* CameraName)
{
	m_PreCamera = m_ActiveCamera;

	//check if not 0 so you dont activate state again;
	if (m_PreCamera != 0)
	{
		m_ActiveCamera->OnDeActive();
	}

	m_ActiveCamera = m_Cameras[CameraName];

	//if changing states
	if (m_PreCamera != m_ActiveCamera)
	{
		m_ActiveCamera->OnActive();
	}
}

bool CameraManager::AddCamera(const std::string& CameraName, ICamera* cam)
{
	CameraMap::iterator It = m_Cameras.find(CameraName);
	if(It == m_Cameras.end())
	{
		m_Cameras[CameraName] = cam;
		return true;
	}

	else
	{
		std::string Error = "Camera Already Exists: " + It->first;
		ReportError(Error);
		return false;
	}
}

void CameraManager::BuildCameraProjectionMatrix(float fov, float aspect, float nearZ, float farZ)
{
	for(CameraMap::iterator it = m_Cameras.begin(); it != m_Cameras.end(); it++)
	{
		it->second->BuildProjectionMatrix(fov, aspect, nearZ, farZ);
	}
}
